package com.jz;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: JZ62 孩子们的游戏(圆圈中最后剩下的数)
 *
 * @author weiruibai.vendor
 * Date: 2022/9/23 10:46
 */
public class JZ62 {

    public static void main(String[] args) {
        int n = 10;
        int m = 17;
        //
        n = 2;
        m = 3;

        n = 5;
        m = 3;
        JZ62 jz62 = new JZ62();
        System.out.println(jz62.LastRemaining_Solution(n, m));
    }

    public int LastRemaining_Solution(int n, int m) {
        List<Node> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(new Node(i));
        }
        int index = 0;
        while ((n = list.size()) != 1) {
            index = (index + m) % n;
            if (index == 0) {
                list.remove(list.size() - 1);
            } else {
                list.remove(--index);
            }
        }
        return list.get(0).number;
    }

    class Node {
        public int number;

        public Node(int number) {
            this.number = number;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "number=" + number +
                    '}';
        }
    }


}

